package com.fin.zw.aiqas.service;

import com.fin.zw.aiqas.entity.Converse;
import com.fin.zw.aiqas.entity.RuleInfo;
import com.fin.zw.aiqas.model.request.ConverseRequest;
import com.fin.zw.aiqas.repository.ConverseRepository;
import com.fin.zw.aiqas.util.BadRequestException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: ZhangGQ
 * @Date: 2019/9/5 16:29
 */
@Service("converseService")
public class ConverseService {

    @Autowired
    private ConverseRepository converseRepository;
    @Autowired
    private ConverseRuleService converseRuleService;

    public void add(Converse converse){
        converseRepository.save(converse);
    }

    public void addAll(List<Converse> list){
        converseRepository.saveAll(list);
    }


    /**
     * 对文本信息进行复核复议审批
     *
     * @param request
     */
    public String updateConverseRule(ConverseRequest request) {
        Optional<Converse> optional = converseRepository.findById(request.getId());
        if (!optional.isPresent()) {
            throw new BadRequestException("converseApproval", "修改的文本信息不存在!");
        }
        Converse converse = optional.get();
        //去除对应的ruleInfo
        converseRuleService.removeRuleInfo(converse.getId(), request.getRuleId());
        Set<RuleInfo> ruleInfoSet = converseRuleService.getRuleInfoByConverseId(converse.getId());
        List<String> stringList = ruleInfoSet.stream().map(RuleInfo::getRuleName).collect(Collectors.toList());
        String string = StringUtils.join(stringList, ",");
        return string;
    }
}
